package swordoffer.offer06;

import newcoder_左神算法课源码.linkedList.Code02_ReverseList;

import java.util.ArrayList;
import java.util.Collections;

public class ReverseSolution {
    public static void main(String[] args) {
        ListNode head = new ListNode(0);
        head.next = new ListNode(1);
        head.next.next = new ListNode(2);
        printLinkedList(head);
        printListFromTailToHead(head);
        printLinkedList(head);
    }
    public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> res = new ArrayList<>();
        if(listNode == null){
            return res;
        }
        ListNode temp = new ListNode(-1);
        while(listNode != null){
            ListNode next = listNode.next;
            listNode.next = temp.next;
            temp.next = listNode;
            listNode = next;
        }
        ListNode cur = temp.next;
        while(cur != null){
            res.add(cur.val);
            cur = cur.next;
        }
        return res;
    }
    public static void printLinkedList(ListNode head) {
        System.out.print("Linked List: ");
        while (head != null) {
            System.out.print(head.val + " ");
            head = head.next;
        }
        System.out.println();
    }
}
